﻿using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using WpfApp.ViewModels;

namespace WpfApp.Views
{
    /// <summary>
    /// BindView.xaml 的交互逻辑
    /// </summary>
    public partial class BindView : Window
    {
        BindViewModel bindViewModel = new BindViewModel() { Number1 = 10, Number2 = 10, Number3 = 10 };
        public BindView()
        {
            InitializeComponent();
            //创建绑定,也可直接指定到控件
            DataContext = bindViewModel;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //创建线程修改数据
            Task.Run(() =>
            {
                while (true)
                {
                    bindViewModel.Number3++;
                    if (bindViewModel.Number3 > 50)
                        bindViewModel.Number3 = 0;
                    Thread.Sleep(1000);
                }
            });
            //Number1修改时，给Number2赋值
            Task.Run(() =>
            {
                while (true)
                {
                    bindViewModel.Number2 = bindViewModel.Number1;
                    Thread.Sleep(100);
                }
            });
        }
    }
}
